<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Category_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
	
    function get_all_data($num=20,$offset=0,$arr_search)
    {
		/*Begin search*/
		switch($arr_search['field_search'])
		{
			case 'catname_vn':
				$this->db->like('catname_vn',$arr_search['key_search']);
				break ;
		}
		/*End search*/
		
		$this->db->where('bl_active <>', -1);
		$this->db->order_by('nb_order','ASC');
		$query = $this->db->get('category',$num,$offset);
		return $query->result();
    }
	
    function get_num_data($arr_search)
	{
		/*Begin search*/
		switch($arr_search['field_search'])
		{
			case 'catname_vn':
				$this->db->like('catname_vn',$arr_search['key_search']);
				break ;
		}
		/*End search*/
		$this->db->where('bl_active <>',-1);
		$query = $this->db->get('category');
		return $query->num_rows();
    }

	function getCategory($catsub=0)
	{	
		$this->db->where(array("catsub"=>$catsub,"bl_active"=>1));
        $query = $this->db->get('category');
        return $query->result();
    }
	
    function get_detail($id)
    {
        $query = $this->db->get_where('category', array('catid' =>$id));
        return $query->row();
    }
	
	function optionCategoryLevel($cid=0, $value='', $nb_level=4, $pace="|")
	{
		$str = '&nbsp;';
		$this->db->select('catid as no, catname_vn as name, catsub') ;
		$query =  $this->db->get_where('category', array("catsub"=>$cid,'bl_active !=' =>-1));
		$data= $query->result();
		
		if($data):
			--$nb_level;
			$pace .= "__";
			foreach($data as $object):
				$str .= "<option value='".$object->no."'".($object->no==$value?" selected=\"selected\"":"").">" . $pace . $object->name. "</option>";
				$str .= $this->optionCategoryLevel($object->no, $value, $nb_level, $pace);
			endforeach;
		endif;
		return $str;
	}
	
    function get_list_permit()
	{
        return $this->db->get('permission')->result();
    }
	
	function optionCategory($value=0)
	{
		$query = $this->db->get('category');
		$category_arr = $query->result();
		$str_opt = "";
		$str_opt = "<option value=''> Chuyên mục gốc</option>";
		foreach($category_arr as $item_opt){
			$str_opt .="<option value='".$item_opt->catid."'".(($value==$item_opt->catid)?" selected":"")."> ".$item_opt->name."</option>";
		}
		return $str_opt;
	}

   function save($data, $catid=0)
   {
		if($catid){
			$this->db->where('catid',$catid);
			if($this->db->update('category',$data)){
				 return true;
			}else{
				 return false;
			}
		}else{
			if($this->db->insert('category',$data)){
				return true;
			}else{
				return false;
			}
		}
    }
	
    function delete($id)
	{
          $this->db->where('catid',$id);
          $query = $this->db->delete('category');
          if($query){
              return true;
          }else{
              return false;
          }
    }
	
	function deleted($id)
	{
          $this->db->where('catid',$id);
		  $data=array('bl_active'=>'-1');
          $query = $this->db->update('category',$data);
          if($query){
              return true;
          }else{
              return false;
          }
    }
	
}
?>
